﻿// -----------------------------------------------------------------------
// <copyright file="ArrayByteExtensions.cs" company="Henadzi Sabaleuski.">
// Copyright (c) Henadzi Sabaleuski. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace HSabaleuski.Utilities.DataTypes.ExtensionMethods
{
	#region Usings
	using System;
	using System.Text;

	#endregion

	/// <summary>
	/// array of bytes type extension methods
	/// </summary>
	public static class ArrayByteExtensions
	{
		#region Functions

		#region ToBase64String

		/// <summary>
		/// Converts a byte array into a base 64 string
		/// </summary>
		/// <param name="input">Input array</param>
		/// <returns>
		/// The equivalent byte array in a base 64 string
		/// </returns>
		public static string ToBase64String(this byte[] input)
		{
			return input == null ? string.Empty : Convert.ToBase64String(input);
		}

		#endregion

		#region ToEncodedString

		/// <summary>
		/// Converts a byte array to a string
		/// </summary>
		/// <param name="input">input array</param>
		/// <param name="usingEncoding">The type of encoding the string is using (defaults to UTF8)</param>
		/// <param name="index">The index.</param>
		/// <param name="count">Number of bytes starting at the index to convert (use -1 for the entire array starting at the index)</param>
		/// <returns>
		/// string of the byte array
		/// </returns>
		public static string ToEncodedString(this byte[] input, Encoding usingEncoding, int index, int count)
		{
			if (input == null)
			{
				return string.Empty;
			}

			if (count == -1)
			{
				count = input.Length - index;
			}
			
			usingEncoding = usingEncoding ?? new UTF8Encoding();

			return usingEncoding.GetString(input, index, count);
		}

		/// <summary>
		/// Converts a byte array to a string
		/// </summary>
		/// <param name="input">input array</param>
		/// <returns>
		/// string of the byte array
		/// </returns>
		public static string ToEncodedString(this byte[] input)
		{
			if (input == null)
			{
				return string.Empty;
			}

			return input.ToEncodedString(null);
		}

		/// <summary>
		/// Converts a byte array to a string
		/// </summary>
		/// <param name="input">input array</param>
		/// <param name="usingEncoding">The using encoding.</param>
		/// <returns>
		/// string of the byte array
		/// </returns>
		public static string ToEncodedString(this byte[] input, Encoding usingEncoding)
		{
			if (input == null)
			{
				return string.Empty;
			}

			return input.ToEncodedString(usingEncoding, 0);
		}

		/// <summary>
		/// Converts a byte array to a string
		/// </summary>
		/// <param name="input">input array</param>
		/// <param name="usingEncoding">The using encoding.</param>
		/// <param name="index">The index.</param>
		/// <returns>
		/// string of the byte array
		/// </returns>
		public static string ToEncodedString(this byte[] input, Encoding usingEncoding, int index)
		{
			if (input == null)
			{
				return string.Empty;
			}

			return input.ToEncodedString(usingEncoding, index, -1);
		}

		#endregion

		#endregion
	}
}